package com.gxa.pingan.service.impl;

import com.gxa.pingan.common.ExceptionCode;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.mapper.SysPointMapper;
import com.gxa.pingan.pojo.SysPoint;
import com.gxa.pingan.service.SysPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Throwable.class)
public class SysPointServiceImpl implements SysPointService {

    @Autowired
    private SysPointMapper sysPointMapper = null;

    @Override
    public void add(SysPoint sysPoint) throws BusinessException {
        if (sysPoint == null) {
            throw new BusinessException(ExceptionCode.PARAM_ERROR);
        }
        Integer add = sysPointMapper.add(sysPoint);
        if (add == 0) {
            throw new BusinessException(ExceptionCode.FAILED_OPERATION);
        }
    }

    @Override
    public void update(SysPoint sysPoint) throws BusinessException {
        if (sysPoint == null || sysPoint.getId() == null) {
            throw new BusinessException(ExceptionCode.PARAM_ERROR);
        }
        if (sysPointMapper.findById(sysPoint.getId()) == null) {
            throw new BusinessException(ExceptionCode.NO_DATA);
        }
        Integer update = sysPointMapper.update(sysPoint);
        if (update == 0) {
            throw new BusinessException(ExceptionCode.FAILED_OPERATION);
        }
    }

    @Override
    public void delete(Long id) throws BusinessException {
        if (sysPointMapper.findById(id) == null) {
            throw new BusinessException(ExceptionCode.NO_USER);
        }
        Integer delete = sysPointMapper.delete(id);
        if (delete == null) {
            throw new BusinessException(ExceptionCode.FAILED_OPERATION);
        }
    }

    @Override
    public List<SysPoint> selectByPage(Integer page, Integer size) {
        if (page == null || size == null) {
            page = 1;
            size = 10;
        }
        Integer offset = (page - 1) * size;
        return sysPointMapper.selectByPage(offset,size);
    }

    @Override
    public Integer dataCount() {
        return sysPointMapper.selectByPage(null,null).size();
    }
}
